<template>
  <el-dialog
    title="挂单条目列表"
    :close-on-click-modal="false"
    center
    width="70%"
    :visible.sync="visible">
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="danger" @click="someCheckOut" :disabled="dataListSelections.length <= 0">已选结账</el-button>
        <el-button @click="allCheckOut" type="success">全部结账</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="">
      </el-table-column>
      <el-table-column
        prop="orderId"
        header-align="center"
        align="center"
        label="订单ID">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="accountId"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="单位">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="money"
        header-align="center"
        align="center"
        label="挂账金额">
        <template slot-scope="scope">
          <div style="color: red">￥{{scope.row.money}}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 1">堂吃</el-tag>
          <el-tag v-if="scope.row.type === 2">快餐</el-tag>
          <el-tag v-if="scope.row.type === 3">外卖</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1">未结</el-tag>
          <el-tag v-if="scope.row.status === 2" type="danger">已结</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="foodVo"
        header-align="center"
        align="center"
        width="300px"
        label="菜品集合">
        <template slot-scope="scope">
          <div v-for="item in JSON.parse(scope.row.foodVo)">
            {{ item.foodName }}/{{ item.foodNum }}份
            <font style="color: red;">￥{{ item.total }}元</font>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="settlementLink"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="结账人">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="settlementTel"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="结账人电话">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="settlementTime"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="结账时间">-->
<!--      </el-table-column>-->

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
<!--          <el-button type="text" v-if="scope.row.status === 1" size="small" @click="addOrUpdateHandle(scope.row.id)">结账</el-button>-->
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>

    <el-dialog title="结款" :visible.sync="checkOutVisible" append-to-body center>
      <el-form :model="checkOutForm" ref="dataForm" label-width="120px">
        <el-form-item label="结款人" prop="accountUnitName">
          <el-input  v-model="checkOutForm.settlementLink" placeholder="结款人"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="accountUnitTel">
          <el-input v-model="checkOutForm.settlementTel" placeholder="电话"></el-input>
        </el-form-item>
        <el-form-item label="支付方式" prop="payMethod">
          <el-select v-model="checkOutForm.payMethod" placeholder="支付方式">
            <el-option label="现金" value="现金"></el-option>
            <el-option label="支付宝" value="支付宝"></el-option>
            <el-option label="微信" value="微信"></el-option>
            <el-option label="银行卡" value="银行卡"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="checkOutVisible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
  import {isMobile} from "../../../../../utils/validate";

  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        id:'',
        visible:false,
        checkOutForm:{

        },
        checkOutVisible:false,
        disabledSubmit:false,
        allCheckSign:''
      }
    },
    components: {

    },
    activated () {
      this.getDataList()
    },
    methods: {
      //全部结账
      allCheckOut(){
        if (this.dataList.length < 1){
          this.$message.info("当前无账单")
        }else {
          this.checkOutVisible = true
          this.allCheckSign = 1
        }

      },

      someCheckOut(){
        this.checkOutVisible = true
        this.allCheckSign = ''
      },

      //结账
      dataFormSubmit(){
        if (!isMobile(this.checkOutForm.settlementTel) || this.checkOutForm.settlementLink === ''
         || this.checkOutForm.payMethod === ''){
          this.$message.info("请保证电话和联系人正确")
        }else {
          this.disabledSubmit = true
          let paramsArray = []
          this.dataListSelections.forEach(item => {
            let param = {
              id:item.id,
              money:item.money,
            }
            paramsArray.push(param)
          })
          this.$http({
            url: this.$http.adornUrl(`/store/tstoreaccountlist/${this.allCheckSign===1?'allCheckOut':'checkOut'}`),
            method: 'post',
            data: this.$http.adornData({
              checkOutList:paramsArray,
              settlementTel: this.checkOutForm.settlementTel,
              settlementLink: this.checkOutForm.settlementLink,
              accountId:this.id,
              payMethod:this.checkOutForm.payMethod
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.checkOutVisible = false
                  this.$emit('refreshDataList')
                  this.disabledSubmit = false
                }
              })
            } else {
              this.disabledSubmit = false
              this.$message.error(data.msg)
            }
          })
        }

      },

      // 获取数据列表
      getDataList (id) {
        this.visible = true
        this.id = id
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/store/tstoreaccountlist/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'accountId':id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList(this.id)
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList(this.id)
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/store/tstoreaccountlist/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
